package com.scpii.universal.resource;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.protobuf.InvalidProtocolBufferException;
import com.scpii.universal.dispatch.Dispatcher;
import com.scpii.universal.pb.UniversalProtocol.Cmd;
import com.scpii.universal.util.Conversion;
import com.scpii.universal.util.StreamUtil;

@RequestMapping("/resources/rest")
@Component
public class DataQueryResource {

	private static final ResponseEntity<byte[]> NO_CONTENT = new ResponseEntity<byte[]>(
			HttpStatus.NO_CONTENT);

	private static final Logger log = LoggerFactory
			.getLogger(DataQueryResource.class);

	@Resource
	private Dispatcher dispatcher;

	@RequestMapping(method = RequestMethod.GET, produces = { "application/octet-stream" })
	public ResponseEntity<byte[]> getData(@RequestParam("cmd") Integer cmd,
			@RequestParam("data") String data, HttpServletRequest request) {
		byte[] reqData = data != null && data.length() > 0 ? Conversion
				.hexStringToBytes(data) : null;
		return execute(cmd, reqData, request);
	}

	@RequestMapping(method = RequestMethod.POST, produces = { "application/octet-stream" })
	public ResponseEntity<byte[]> getDataPost(@RequestParam("cmd") Integer cmd,
			HttpServletRequest request) throws Exception {
		byte[] reqData = StreamUtil.convertToBytes(request.getInputStream());
		return execute(cmd, reqData, request);
	}

	private ResponseEntity<byte[]> execute(Integer cmd, byte[] reqData,
			HttpServletRequest request) {
		try {
			if (cmd == null) {
				return NO_CONTENT;
			}
			byte[] content = generate(cmd, reqData != null
					&& reqData.length > 0 ? reqData : null);
			ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(
					content, HttpStatus.OK);
			return response;
		} catch (Exception e) {
			return NO_CONTENT;
		}
	}

	public byte[] generate(int cmd, byte[] data) {
		byte[] content = null;
		switch (Cmd.valueOf(cmd)) {
		case CreatComplieTask:// 创建编译任务
			log.info("创建编译任务开始");
			try {
				content = dispatcher.addCompileTask(data);
			} catch (InvalidProtocolBufferException e) {
				log.error("创建编译任务请求数据格式转换出错,错误原因：" + e.getMessage());
			}
			log.info("创建编译任务结束");
			break;
		case QryFinishComplieTask:// 获取编译完成任务
			log.info("获取编译完成任务开始");
			try {
				content = dispatcher.getTaskByAppUpdateId(data);
			} catch (InvalidProtocolBufferException e) {
				log.error("获取编译完成任务请求数据格式转换出错,错误原因：" + e.getMessage());
			}
			log.info("获取编译完成任务结束");
			break;
		case GetTask:// 请求编译任务接口的请求
			log.info("请求编译任务接口的请求开始");
			try {
				content = dispatcher.getWaitCompileTask(data);
			} catch (InvalidProtocolBufferException e) {
				log.error("请求编译任务接口请求数据格式转换出错,错误原因：" + e.getMessage());
			}
			log.info("请求编译任务接口的请求结束");
			break;
		case FinishTask:// 完成编译任务接口的参数请求
			log.info("完成编译任务接口的参数请求开始");
			try {
				content = dispatcher.dealFinishCompileTask(data);
			} catch (InvalidProtocolBufferException e) {
				log.error("完成编译任务接口请求数据格式转换出错,错误原因：" + e.getMessage());
			}
			log.info("完成编译任务接口的参数请求结束");
			break;

		default:
			break;
		}

		return content;
	}
}
